// 构建整个代码块的初始文件夹及代码文件
// node ./init.mjs

import fs from 'fs';

fs.mkdirSync(`./codes`);

const jsTempData = fs.readFileSync('./template/main.js', 'utf-8');
const tsTempData = fs.readFileSync('./template/main.ts', 'utf-8');
const rsTempData = fs.readFileSync('./template/main.rs', 'utf-8');
const rmTempData = fs.readFileSync('./template/README.md', 'utf-8');

function format(num) {
    if (num < 10) {
        return `000${num}`;
    } else if (num < 100) {
        return `00${num}`;
    } else if (num < 1000) {
        return `0${num}`;
    } else {
        return `${num}`;
    }
}

for (let i = 0; i < 20; i++) {
    const level1 = `${format(i * 100 + 1)}-${format((i + 1) * 100)}`;
    fs.mkdirSync(`./codes/${level1}`);
    for (let j = 0; j < 10; j++) {
        const level2 = `${format(i * 100 + j * 10 + 1)}-${format(i * 100 + (j + 1) * 10)}`;
        fs.mkdirSync(`./codes/${level1}/${level2}`);
        for (let k = 1; k <= 10; k++) {
            const level3 = `${format(i * 100 + j * 10 + k)}`;
            fs.mkdirSync(`./codes/${level1}/${level2}/${level3}`);
            fs.writeFileSync(`./codes/${level1}/${level2}/${level3}/main.js`, jsTempData, 'utf-8');
            fs.writeFileSync(`./codes/${level1}/${level2}/${level3}/main.ts`, tsTempData, 'utf-8');
            fs.writeFileSync(`./codes/${level1}/${level2}/${level3}/main.rs`, rsTempData, 'utf-8');
            fs.writeFileSync(`./codes/${level1}/${level2}/${level3}/README.md`, rmTempData, 'utf-8');
        }
    }
}

